package com.hengtongxing.hejiayun.shop.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.utils.DisplayUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class ShopDetailsImgListAdapter extends RecyclerView.Adapter<ShopDetailsImgListAdapter.ViewHolder> {


    private List<String> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;
    private int imageWidth;

    public ShopDetailsImgListAdapter(List<String> listsBeans) {
        this.listsBeans = listsBeans;
        imageWidth = DisplayUtil.getScreenWidth();
    }

    public void setOnCallBackListener(ShopDetailsImgListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    private RequestOptions requestOptions = new RequestOptions()//
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .format(DecodeFormat.PREFER_RGB_565)
            .placeholder(R.color.bgColor_default)//
            .error(R.color.bgColor_default);

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_shop_details_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String dataBean = listsBeans.get(position);
        Glide.with(context)
                .setDefaultRequestOptions(requestOptions)
                .asBitmap()
                .load(dataBean)
                .skipMemoryCache(false)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap bitmap, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        int imageHeight = height * imageWidth / width;
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.ivPicture.getLayoutParams();
                        params.height = imageHeight;
                        return false;
                    }
                })
                .into(holder.ivPicture);
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_picture)
        ImageView ivPicture;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onCallBackListener {
        void onCallBack(int type, int position);
    }
}
